package vendas.web.action;

import gminet.infra.business.BusinessException;
import gminet.infra.dao.ImageTrasnferObject;
import gminet.infra.db.TransactionException;
import gminet.infra.web.WebException;

import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import vendas.business.ProductBusiness;
import vendas.dto.ActionType;
import vendas.dto.ActiveStatus;
import vendas.dto.Product;

@ManagedBean(name = "productAction")
@SessionScoped
public class ProductAction extends WebAction {

    private static final long serialVersionUID = -30085647953565310L;

    // private static final String IMAGES_RELATIVE_PATH =
    // "//pages//product//images//";

    private Product product;

    private ImageTrasnferObject imageTO;

    private List<Product> products;

    public ProductAction() {
        super();
    }

    public String listProducts() throws WebException {

        verifyLogin();
        return "listProducts";
    }

    public String prepareInsertProduct() throws WebException {

        verifyLogin();
        setActionType(ActionType.INSERT);
        setProduct(null);
        // setImageTO(null);
        return "editProduct";
    }

    public String insertProduct() throws WebException {

        verifyLogin();
        String pageReturn;
        try {
            // PRODUCT
            ProductBusiness.insert(getProduct(), getImageTO());
            setProducts(null);
            setProduct(null);
            pageReturn = "listProducts";
        } catch (BusinessException be) {
            addBusinessException("frmProduct", be);
            pageReturn = "frmProduct";
        } catch (TransactionException te) {
            throw new WebException(te);
        }
        return pageReturn;
    }

    public String prepareEditProduct() throws WebException {

        verifyLogin();
        String pageReturn = "editProduct";
        setActionType(ActionType.EDIT);
        try {
            int productId = getProduct().getId();
            setProduct(ProductBusiness.findByPK(productId));
        } catch (BusinessException be) {
            addBusinessException("frmListProducts", be);
            pageReturn = "listProducts";
        } catch (TransactionException te) {
            throw new WebException(te);
        }
        return pageReturn;
    }

    public String editProduct() throws WebException {

        verifyLogin();
        String pageReturn;
        try {
            ProductBusiness.update(product);
            setProducts(null);
            setProduct(null);
            pageReturn = "listProducts";
        } catch (BusinessException be) {
            addBusinessException("frmProduct", be);
            pageReturn = "editProduct";
        } catch (TransactionException te) {
            throw new WebException(te);
        }
        return pageReturn;
    }

    public void updateStatusProduct() throws WebException {

        verifyLogin();
        try {
            ActiveStatus activeStatus = null;
            if (getActionType().equals(ActionType.ACTIVE)) {
                activeStatus = ActiveStatus.ACTIVE;
            } else if (getActionType().equals(ActionType.DESACTIVE)) {
                activeStatus = ActiveStatus.INACTIVE;
            }
            if (activeStatus != null) {
                product.setActiveStatus(activeStatus);
                ProductBusiness.updateActiveStatus(product);
                setProducts(null);
                setProduct(null);
            }
        } catch (BusinessException be) {
            addBusinessException("frmListProducts", be);
        } catch (TransactionException te) {
            throw new WebException(te);
        }
    }

    // public void uploadImage(UploadedFile item) throws WebException {
    //
    // this.imageTO = new ImageTrasnferObject();
    // this.imageTO.setData(item.getData());
    // this.imageTO.setLength(item.getData().length);
    // this.imageTO.setRealFileName(item.getName());
    // this.imageTO.setFilePath(getContentRealPath() + IMAGES_RELATIVE_PATH);
    // }

    // public void paintImage(OutputStream stream, Object object) throws
    // IOException {
    // System.out.println(">>>" + object);
    // if (getImageTO() != null) {
    // stream.write(getImageTO().getData());
    // }
    // }

    public void clearUploadImage() {
        setImageTO(null);
    }

    public ImageTrasnferObject getImageTO() {
        if (this.imageTO == null) {
            this.imageTO = new ImageTrasnferObject();
        }
        return imageTO;
    }

    public void setImageTO(ImageTrasnferObject imageTO) {
        this.imageTO = imageTO;
    }

    // public long getTimeStamp() {
    // return System.currentTimeMillis();
    // }

    public Product getProduct() {
        if (this.product == null) {
            this.product = new Product();
        }
        return product;
    }

    public void setProduct(Product product) {
        this.product = product;
    }

    public List<Product> getProducts() throws WebException {
        if (this.products == null) {
            try {
                setProducts(ProductBusiness.findAll());
            } catch (TransactionException te) {
                throw new WebException(te);
            }
        }
        return products;
    }

    public void setProducts(List<Product> products) {
        this.products = products;
    }

}
